﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.IO;
using System.Drawing;
using WindowsInstaller;
using System.Threading;
using System.Collections;
using Microsoft.Win32;

namespace ISUM_V2
{
    /// <summary>
    /// UCMacro.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class UCMacro : UserControl
    {

        private MacroType _MacroType = null;

        private MorseCodeFileModule _Mcfm { get; set; }

        private List<string> _TempAtoL { get; set; }
        public UCMacro ()
        {
            InitializeComponent();


            //_MacroType = new MacroType();
            //_MacroType._QtoP = null;
            //_MacroType._AtoL = null;
            //_MacroType._ZtoM = null;
            //_MacroFunction = new MacroFunction();


            _Mcfm = MorseCodeFileModule.getInstance();



            Cb_ProcessList_A.SelectionChanged += new SelectionChangedEventHandler( Cb_ProcessList_A_SelectionChanged );
            Cb_ProcessList_S.SelectionChanged += new SelectionChangedEventHandler( Cb_ProcessList_S_SelectionChanged );
            Cb_ProcessList_D.SelectionChanged += new SelectionChangedEventHandler( Cb_ProcessList_D_SelectionChanged );
            Cb_ProcessList_F.SelectionChanged += new SelectionChangedEventHandler( Cb_ProcessList_F_SelectionChanged );
            Cb_ProcessList_G.SelectionChanged += new SelectionChangedEventHandler( Cb_ProcessList_G_SelectionChanged );
            Cb_ProcessList_H.SelectionChanged += new SelectionChangedEventHandler( Cb_ProcessList_H_SelectionChanged );
            Cb_ProcessList_J.SelectionChanged += new SelectionChangedEventHandler( Cb_ProcessList_J_SelectionChanged );
            Cb_ProcessList_K.SelectionChanged += new SelectionChangedEventHandler( Cb_ProcessList_K_SelectionChanged );
            Cb_ProcessList_L.SelectionChanged += new SelectionChangedEventHandler( Cb_ProcessList_L_SelectionChanged );

            //Btn_OK.Click += new RoutedEventHandler( Btn_OK_Click );
            //Btn_Cancel.Click += new RoutedEventHandler( Btn_Cancel_Click );
            Btn_Apply.Click += new RoutedEventHandler( Btn_Apply_Click );
            Btn_initMacro.Click += new RoutedEventHandler( Btn_initMacro_Click );
            Btn_QtoP.Click += new RoutedEventHandler( Btn_QtoP_Click );
            Btn_AtoL.Click += new RoutedEventHandler( Btn_AtoL_Click );
            Btn_ZtoM.Click += new RoutedEventHandler( Btn_ZtoM_Click );

            ShowCanvasQtoP();
            //LoadValues();
            //RoadValues();
            //Cb_First_Q.SelectionChanged += new SelectionChangedEventHandler( Cb_First_Q_SelectionChanged );
        }

        void Btn_initMacro_Click ( object sender, RoutedEventArgs e )
        {
            _MacroType = new MacroType();
            
            _Mcfm.writeFile( "../../Macro.data", _MacroType );

            LoadMacro();
        }

        void Btn_Apply_Click ( object sender, RoutedEventArgs e )
        {
            SaveMacro();
        }

        void Btn_ZtoM_Click ( object sender, RoutedEventArgs e )
        {
            ShowCanvasZtoM();
        }

        void Btn_AtoL_Click ( object sender, RoutedEventArgs e )
        {
            ShowCanvasAtoL();
            SearchInstalledProcessList();
        //    LoadValues();
        }

        void Btn_QtoP_Click ( object sender, RoutedEventArgs e )
        {
            ShowCanvasQtoP();
        }


        void Cb_ProcessList_L_SelectionChanged ( object sender, SelectionChangedEventArgs e )
        {
            ImageSource ImgSource = ( (System.Windows.Controls.Image)( Cb_ProcessList_L.SelectedValue as DockPanel ).Children[ 0 ] ).Source;

            if ( ImgSource != null )
                Img_L.Source = ImgSource;
        }

        void Cb_ProcessList_K_SelectionChanged ( object sender, SelectionChangedEventArgs e )
        {
            ImageSource ImgSource = ( (System.Windows.Controls.Image)( Cb_ProcessList_K.SelectedValue as DockPanel ).Children[ 0 ] ).Source;

            if ( ImgSource != null )
                Img_K.Source = ImgSource;
        }

        void Cb_ProcessList_J_SelectionChanged ( object sender, SelectionChangedEventArgs e )
        {
            ImageSource ImgSource = ( (System.Windows.Controls.Image)( Cb_ProcessList_J.SelectedValue as DockPanel ).Children[ 0 ] ).Source;

            if ( ImgSource != null )
                Img_J.Source = ImgSource;
        }

        void Cb_ProcessList_H_SelectionChanged ( object sender, SelectionChangedEventArgs e )
        {
            ImageSource ImgSource = ( (System.Windows.Controls.Image)( Cb_ProcessList_H.SelectedValue as DockPanel ).Children[ 0 ] ).Source;

            if ( ImgSource != null )
                Img_H.Source = ImgSource;
        }

        void Cb_ProcessList_G_SelectionChanged ( object sender, SelectionChangedEventArgs e )
        {
            ImageSource ImgSource = ( (System.Windows.Controls.Image)( Cb_ProcessList_G.SelectedValue as DockPanel ).Children[ 0 ] ).Source;

            if ( ImgSource != null )
                Img_G.Source = ImgSource;
        }

        void Cb_ProcessList_F_SelectionChanged ( object sender, SelectionChangedEventArgs e )
        {
            ImageSource ImgSource = ( (System.Windows.Controls.Image)( Cb_ProcessList_F.SelectedValue as DockPanel ).Children[ 0 ] ).Source;

            if ( ImgSource != null )
                Img_F.Source = ImgSource;
        }

        void Cb_ProcessList_D_SelectionChanged ( object sender, SelectionChangedEventArgs e )
        {
            ImageSource ImgSource = ( (System.Windows.Controls.Image)( Cb_ProcessList_D.SelectedValue as DockPanel ).Children[ 0 ] ).Source;

            if ( ImgSource != null )
                Img_D.Source = ImgSource;
        }

        void Cb_ProcessList_S_SelectionChanged ( object sender, SelectionChangedEventArgs e )
        {
            ImageSource ImgSource = ( (System.Windows.Controls.Image)( Cb_ProcessList_S.SelectedValue as DockPanel ).Children[ 0 ] ).Source;

            if ( ImgSource != null )
                Img_S.Source = ImgSource;
        }

        void Cb_ProcessList_A_SelectionChanged ( object sender, SelectionChangedEventArgs e )
        {
            ImageSource ImgSource = ( (System.Windows.Controls.Image)( Cb_ProcessList_A.SelectedValue as DockPanel ).Children[ 0 ] ).Source;

            if ( ImgSource != null )
                Img_A.Source = ImgSource;
        }
        private void SearchInstalledProcessList2 ()
        {
            string Software = null;

            string SoftwareKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";


            using ( RegistryKey rk = Registry.LocalMachine.OpenSubKey( SoftwareKey ) )
            {
                foreach ( string skName in rk.GetSubKeyNames() )
                {
                    using ( RegistryKey sk = rk.OpenSubKey( skName ) )
                    {
                        try
                        {
                            if ( !( sk.GetValue( "DisplayName" ) == null ) )
                            {
                                if ( sk.GetValue( "InstallLocation" ) == null )
                                {
                                    // Can`t find
                                    Software += sk.GetValue( "DisplayName" ) + " - Install path not known\n";
                                }

                                else
                                {
                                    string rootDirectory = sk.GetValue( "InstallLocation" ).ToString();

                                    string[] files = System.IO.Directory.GetFiles( rootDirectory, "*.exe", System.IO.SearchOption.AllDirectories );

                                    foreach ( string name in files )
                                    {
                                        // AddAllProcessList( name );
                                        //Console.WriteLine( name );
                                        //comboBox1.Items.Add( asdf );
                                    }
                                }
                            }
                        }
                        catch ( Exception ex )
                        {
                            Console.WriteLine( ex.ToString() );
                        }
                    }
                }
            }
        }

        private void SearchInstalledProcessList ()
        {
            // ClearAllProcessList();
            int index = 1;
            
            Installer wi = (Installer)Activator.CreateInstance( Type.GetTypeFromProgID( "WindowsInstaller.Installer" ) );
            StringList sl = wi.Products;

            
                AddAllProcessList( "C:/windows/notepad.exe", index++ );
                //AddAllProcessList( "C:/Program Filesh/Microsoft Office/Office14/WINWORD.exe", index++ );
               
          
            foreach ( string pn in sl )
            {
                string path = wi.get_ProductInfo( pn, "InstallLocation" );
                Console.WriteLine( path );

                if ( !path.Equals( "" ) && !path.Equals("c:/windows/system32" ))
                {
                    DirectoryInfo directoryInfo = new DirectoryInfo( path );

                    try
                    {
                        string[] filename = Directory.GetFiles( path, "*.exe" );

                        if ( filename.Length != 0 )
                        {
                            foreach ( string name in filename )
                            {
                                AddAllProcessList( name, index++ );
                                Console.WriteLine( name );
                            }
                           
                        
                        }
                            }
                    catch ( Exception ex )
                    {
                        Console.WriteLine( ex.ToString() );
                    }
                }
            }
        }

        private ImageSource GetProcessItemSource ( string targetName )
        {

            BitmapImage bi = new BitmapImage();
            MemoryStream ms = new MemoryStream();
            System.Windows.Controls.Image img = new System.Windows.Controls.Image();

            System.Drawing.Icon i = System.Drawing.Icon.ExtractAssociatedIcon( targetName );
            Bitmap bmp = i.ToBitmap();

            bmp.Save( ms, System.Drawing.Imaging.ImageFormat.Png );

            bi.BeginInit();
            ms.Seek( 0, SeekOrigin.Begin );

            bi.CacheOption = BitmapCacheOption.OnLoad;
            bi.StreamSource = ms;
            bi.DecodePixelHeight = 30;
            bi.DecodePixelWidth = 30;
            bi.EndInit();
            bi.StreamSource.Dispose();

            img.Source = bi;
            img.Stretch = Stretch.Fill;


            bmp.Dispose();
            bmp = null;
            ms.Dispose();
            ms = null;

            return img.Source;
        }
        private void AddProcessItem ( string targetName, ComboBox cb, int index )
        {

            if ( cb.Items.Count == 0 || cb.Items.Count < index )
            {
                DockPanel dp = new DockPanel();
                TextBlock tb = new TextBlock();
                BitmapImage bi = new BitmapImage();
                MemoryStream ms = new MemoryStream();
                System.Windows.Controls.Image img = new System.Windows.Controls.Image();

                System.Drawing.Icon i = System.Drawing.Icon.ExtractAssociatedIcon( targetName );
                Bitmap bmp = i.ToBitmap();

                bmp.Save( ms, System.Drawing.Imaging.ImageFormat.Png );

                bi.BeginInit();
                ms.Seek( 0, SeekOrigin.Begin );

                bi.CacheOption = BitmapCacheOption.OnLoad;
                bi.StreamSource = ms;
                bi.DecodePixelHeight = 30;
                bi.DecodePixelWidth = 30;
                bi.EndInit();
                bi.StreamSource.Dispose();

                img.Source = bi;
                img.Stretch = Stretch.Uniform;

                tb.TextAlignment = TextAlignment.Center;
                tb.FontSize = 14;
                tb.Text = targetName;

                dp.Children.Add( img );
                dp.Children.Add( tb );

                cb.Items.Add( dp );

                bmp.Dispose();
                bmp = null;
                ms.Dispose();
                ms = null;
            }
        }

        private void ShowCanvasQtoP ()
        {
            Canvas_QtoP.Visibility = Visibility.Visible;
            Canvas_AtoL.Visibility = Visibility.Hidden;
            Canvas_ZtoM.Visibility = Visibility.Hidden;

            Btn_QtoP.Opacity = 1;
            Btn_AtoL.Opacity = 0.5;
            Btn_ZtoM.Opacity = 0.5;
        }

        private void ShowCanvasAtoL ()
        {
            Canvas_QtoP.Visibility = Visibility.Hidden;
            Canvas_AtoL.Visibility = Visibility.Visible;
            Canvas_ZtoM.Visibility = Visibility.Hidden;

            Btn_QtoP.Opacity = 0.5;
            Btn_AtoL.Opacity = 1;
            Btn_ZtoM.Opacity = 0.5;
        }

        private void ShowCanvasZtoM ()
        {
            Canvas_QtoP.Visibility = Visibility.Hidden;
            Canvas_AtoL.Visibility = Visibility.Hidden;
            Canvas_ZtoM.Visibility = Visibility.Visible;

            Btn_QtoP.Opacity = 0.5;
            Btn_AtoL.Opacity = 0.5;
            Btn_ZtoM.Opacity = 1;
        }


        private void AddAllProcessList ( String targetName, int index )
        {

            AddProcessItem( targetName, Cb_ProcessList_A, index );
            AddProcessItem( targetName, Cb_ProcessList_S, index );
            AddProcessItem( targetName, Cb_ProcessList_D, index );
            AddProcessItem( targetName, Cb_ProcessList_F, index );
            AddProcessItem( targetName, Cb_ProcessList_G, index );
            AddProcessItem( targetName, Cb_ProcessList_H, index );
            AddProcessItem( targetName, Cb_ProcessList_J, index );
            AddProcessItem( targetName, Cb_ProcessList_K, index );
            AddProcessItem( targetName, Cb_ProcessList_L, index );
        }

        private void ClearAllProcessList ()
        {
            Cb_ProcessList_A.Items.Clear();
            Cb_ProcessList_S.Items.Clear();
            Cb_ProcessList_D.Items.Clear();
            Cb_ProcessList_F.Items.Clear();
            Cb_ProcessList_G.Items.Clear();
            Cb_ProcessList_H.Items.Clear();
            Cb_ProcessList_J.Items.Clear();
            Cb_ProcessList_K.Items.Clear();
            Cb_ProcessList_L.Items.Clear();
        }
        private void SaveQtoP ()
        {
            _MacroType._QtoP.Clear();
            _MacroType.AddQtoP( SaveModuleQtoP( Cb_First_Q ), SaveModuleQtoP( Cb_Second_Q ), SaveModuleQtoP( Cb_Third_Q ), SaveModuleQtoP( Cb_Fourth_Q ) );
            _MacroType.AddQtoP( SaveModuleQtoP( Cb_First_W ), SaveModuleQtoP( Cb_Second_W ), SaveModuleQtoP( Cb_Third_W ), SaveModuleQtoP( Cb_Fourth_W ) );
            _MacroType.AddQtoP( SaveModuleQtoP( Cb_First_E ), SaveModuleQtoP( Cb_Second_E ), SaveModuleQtoP( Cb_Third_E ), SaveModuleQtoP( Cb_Fourth_E ) );
            _MacroType.AddQtoP( SaveModuleQtoP( Cb_First_R ), SaveModuleQtoP( Cb_Second_R ), SaveModuleQtoP( Cb_Third_R ), SaveModuleQtoP( Cb_Fourth_R ) );
            _MacroType.AddQtoP( SaveModuleQtoP( Cb_First_T ), SaveModuleQtoP( Cb_Second_T ), SaveModuleQtoP( Cb_Third_T ), SaveModuleQtoP( Cb_Fourth_T ) );
            _MacroType.AddQtoP( SaveModuleQtoP( Cb_First_Y ), SaveModuleQtoP( Cb_Second_Y ), SaveModuleQtoP( Cb_Third_Y ), SaveModuleQtoP( Cb_Fourth_Y ) );
            _MacroType.AddQtoP( SaveModuleQtoP( Cb_First_U ), SaveModuleQtoP( Cb_Second_U ), SaveModuleQtoP( Cb_Third_U ), SaveModuleQtoP( Cb_Fourth_U ) );
            _MacroType.AddQtoP( SaveModuleQtoP( Cb_First_I ), SaveModuleQtoP( Cb_Second_I ), SaveModuleQtoP( Cb_Third_I ), SaveModuleQtoP( Cb_Fourth_I ) );
            _MacroType.AddQtoP( SaveModuleQtoP( Cb_First_O ), SaveModuleQtoP( Cb_Second_O ), SaveModuleQtoP( Cb_Third_O ), SaveModuleQtoP( Cb_Fourth_O ) );
            _MacroType.AddQtoP( SaveModuleQtoP( Cb_First_P ), SaveModuleQtoP( Cb_Second_P ), SaveModuleQtoP( Cb_Third_P ), SaveModuleQtoP( Cb_Fourth_P ) );
        }

        private void SaveAtoL ()
        {
            _MacroType._AtoL.Clear();
            _MacroType.AddAtoL( SaveModuleAtoL( Cb_ProcessList_A, 0 ) );
            _MacroType.AddAtoL( SaveModuleAtoL( Cb_ProcessList_S, 1 ) );
            _MacroType.AddAtoL( SaveModuleAtoL( Cb_ProcessList_D, 2 ) );
            _MacroType.AddAtoL( SaveModuleAtoL( Cb_ProcessList_F, 3 ) );
            _MacroType.AddAtoL( SaveModuleAtoL( Cb_ProcessList_G, 4 ) );
            _MacroType.AddAtoL( SaveModuleAtoL( Cb_ProcessList_H, 5 ) );
            _MacroType.AddAtoL( SaveModuleAtoL( Cb_ProcessList_J, 6 ) );
            _MacroType.AddAtoL( SaveModuleAtoL( Cb_ProcessList_K, 7 ) );
            _MacroType.AddAtoL( SaveModuleAtoL( Cb_ProcessList_L, 8 ) );

        }

        private void SaveZtoM ()
        {
            _MacroType._ZtoM.Clear();
            _MacroType.AddZtoM( SaveModuleZtoM( Tb_MacroText_Z ) );
            _MacroType.AddZtoM( SaveModuleZtoM( Tb_MacroText_X ) );
            _MacroType.AddZtoM( SaveModuleZtoM( Tb_MacroText_C ) );
            _MacroType.AddZtoM( SaveModuleZtoM( Tb_MacroText_V ) );
            _MacroType.AddZtoM( SaveModuleZtoM( Tb_MacroText_B ) );
            _MacroType.AddZtoM( SaveModuleZtoM( Tb_MacroText_N ) );
            _MacroType.AddZtoM( SaveModuleZtoM( Tb_MacroText_M ) );
        }

        private string SaveModuleAtoL ( ComboBox cb, int index )
        {
            if ( cb == null )
                return "";

            else if ( cb.SelectedIndex == -1 && _MacroType._AtoL.Count > index )
            {
                return _TempAtoL[ index ];
            }
            else if ( cb.SelectedIndex == -1 )
            {
                return "";
            }

            DockPanel dp = new DockPanel();
            //System.Windows.Controls.Image img = new System.Windows.Controls.Image();
            TextBlock tb = new TextBlock();

            dp = (DockPanel)cb.SelectedValue;
            //img = (System.Windows.Controls.Image)dp.Children[ 0 ];
            tb = (TextBlock)dp.Children[ 1 ];


            return tb.Text;
        }



        private int SaveModuleQtoP ( ComboBox cb )
        {
            if ( cb == null )
                return -1;

            //return ( cb.SelectedItem as XmlNode ).Attributes[ "Name" ].Value.ToString();
            return cb.SelectedIndex;
        }



        private string SaveModuleZtoM ( TextBox tb )
        {
            if ( tb == null )
                return "";

            return tb.Text;
        }
        public void LoadMacro ()
        {
            string pathMacro = MainWindow.GetDoubleParent() + "/Macro.data";
            if ( File.Exists( pathMacro ) )
            {
                _MacroType = (MacroType)_Mcfm.readFile( pathMacro );
                _TempAtoL = _MacroType._AtoL;
                OnceSetCombine();
                OnceSetImageIcon();
                OnceSetProcessName();
                OnceSetText();
                

            }
            else
            {
                _MacroType = new MacroType();
            }
        }

        
        public delegate void NoParamDelegate ();


        private void SearchInstalledProcessListDelegate ()
        {
            this.Dispatcher.Invoke( new NoParamDelegate( SearchInstalledProcessList ) );
        }
        public void LoadValues ()
        {
         
            
//             new Thread(new ThreadStart(SearchInstalledProcessListDelegate)).Start();

            LoadMacro();
            //ddd

        }


        private void OnceSetText ()
        {
            if ( _MacroType._ZtoM.Count == 7 )
            {
                SetTextBox( Tb_MacroText_Z, _MacroType._ZtoM[ 0 ] );
                SetTextBox( Tb_MacroText_X, _MacroType._ZtoM[ 1 ] );
                SetTextBox( Tb_MacroText_C, _MacroType._ZtoM[ 2 ] );
                SetTextBox( Tb_MacroText_V, _MacroType._ZtoM[ 3 ] );
                SetTextBox( Tb_MacroText_B, _MacroType._ZtoM[ 4 ] );
                SetTextBox( Tb_MacroText_N, _MacroType._ZtoM[ 5 ] );
                SetTextBox( Tb_MacroText_M, _MacroType._ZtoM[ 6 ] );
            }
        }

        private void OnceSetCombine ()
        {
            if ( _MacroType._QtoP.Count == 10 ) 
            {
                SetComboBox( Cb_First_Q, Cb_Second_Q, Cb_Third_Q, Cb_Fourth_Q, _MacroType._QtoP[ 0 ] );
                SetComboBox( Cb_First_W, Cb_Second_W, Cb_Third_W, Cb_Fourth_W, _MacroType._QtoP[ 1 ] );
                SetComboBox( Cb_First_E, Cb_Second_E, Cb_Third_E, Cb_Fourth_E, _MacroType._QtoP[ 2 ] );
                SetComboBox( Cb_First_R, Cb_Second_R, Cb_Third_R, Cb_Fourth_R, _MacroType._QtoP[ 3 ] );
                SetComboBox( Cb_First_T, Cb_Second_T, Cb_Third_T, Cb_Fourth_T, _MacroType._QtoP[ 4 ] );
                SetComboBox( Cb_First_Y, Cb_Second_Y, Cb_Third_Y, Cb_Fourth_Y, _MacroType._QtoP[ 5 ] );
                SetComboBox( Cb_First_U, Cb_Second_U, Cb_Third_U, Cb_Fourth_U, _MacroType._QtoP[ 6 ] );
                SetComboBox( Cb_First_I, Cb_Second_I, Cb_Third_I, Cb_Fourth_I, _MacroType._QtoP[ 7 ] );
                SetComboBox( Cb_First_O, Cb_Second_O, Cb_Third_O, Cb_Fourth_O, _MacroType._QtoP[ 8 ] );
                SetComboBox( Cb_First_P, Cb_Second_P, Cb_Third_P, Cb_Fourth_P, _MacroType._QtoP[ 9 ] );
            }
        }


        private void OnceSetProcessName ()
        {
            if ( _MacroType._AtoL.Count == 9 )
            {
                SetProcessName( Tb_SeletedProcess_A, _MacroType._AtoL[ 0 ] );
                SetProcessName( Tb_SeletedProcess_S, _MacroType._AtoL[ 1 ] );
                SetProcessName( Tb_SeletedProcess_D, _MacroType._AtoL[ 2 ] );
                SetProcessName( Tb_SeletedProcess_F, _MacroType._AtoL[ 3 ] );
                SetProcessName( Tb_SeletedProcess_G, _MacroType._AtoL[ 4 ] );
                SetProcessName( Tb_SeletedProcess_H, _MacroType._AtoL[ 5 ] );
                SetProcessName( Tb_SeletedProcess_J, _MacroType._AtoL[ 6 ] );
                SetProcessName( Tb_SeletedProcess_K, _MacroType._AtoL[ 7 ] );
                SetProcessName( Tb_SeletedProcess_L, _MacroType._AtoL[ 8 ] );
            }
        }
        private void SetProcessName ( TextBlock tb, string str )
        {  
            if(!str.Equals(String.Empty))
            {
                try
                {
                    tb.Text = new FileInfo( str ).Name;
                }
                catch ( Exception ex )
                {
                    Console.WriteLine( ex.Message );                
                }

                
            }
        }
        private void OnceSetImageIcon ()
        {
            if ( _MacroType._AtoL.Count == 9 )
            {
                SetImageIcon( Img_A, _MacroType._AtoL[ 0 ] );
                SetImageIcon( Img_S, _MacroType._AtoL[ 1 ] );
                SetImageIcon( Img_D, _MacroType._AtoL[ 2 ] );
                SetImageIcon( Img_F, _MacroType._AtoL[ 3 ] );
                SetImageIcon( Img_G, _MacroType._AtoL[ 4 ] );
                SetImageIcon( Img_H, _MacroType._AtoL[ 5 ] );
                SetImageIcon( Img_J, _MacroType._AtoL[ 6 ] );
                SetImageIcon( Img_K, _MacroType._AtoL[ 7 ] );
                SetImageIcon( Img_L, _MacroType._AtoL[ 8 ] );
            }
        }

        private void SetTextBox ( TextBox tb, string str )
        {
            tb.Text = str;
        }
        private void SetImageIcon ( System.Windows.Controls.Image image, string str )
        {
            if ( str != String.Empty )
            {
                try
                {
                    image.Source = GetProcessItemSource( str );
                }
                catch ( Exception ex )
                {
                    Console.WriteLine( ex.Message );
                }
            }
        }

        private void SetComboBox ( ComboBox cb1, ComboBox cb2, ComboBox cb3, ComboBox cb4, string str )
        {
            string []arr = str.Split( '+' );

            if ( arr.Length == 4 )
            {
                cb1.SelectedIndex = Convert.ToInt32( arr[ 0 ] );
                cb2.SelectedIndex = Convert.ToInt32( arr[ 1 ] );
                cb3.SelectedIndex = Convert.ToInt32( arr[ 2 ] );
                cb4.SelectedIndex = Convert.ToInt32( arr[ 3 ] );
            }
        }


        public void SaveMacro ()
        {
            SaveQtoP();
            SaveAtoL();
            SaveZtoM();

            _Mcfm.writeFile(MainWindow.GetDoubleParent() + "/Macro.data", _MacroType);

            LoadMacro();
           
        }
    }
}
